package com.cjd.common.utils

import android.annotation.SuppressLint
import android.content.Context
import android.net.ConnectivityManager
import android.net.NetworkInfo

/**
 * @Author chenjidong
 * @email 374122600@qq.com
 * created 2017/6/19.
 * description 网络判断
 */
@SuppressLint("MissingPermission")
object NetUtils{


    /**
     * 判断wifi 是否可用
     *
     * @param context
     * @return Boolean
     */
    fun isWifiDataEnable(context: Context): Boolean {
        val connectivityManager = context
            .getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager

        var isWifiDataEnable = false
        isWifiDataEnable = connectivityManager.getNetworkInfo(
            ConnectivityManager.TYPE_WIFI
        )!!.isConnectedOrConnecting
        return isWifiDataEnable
    }

    /**
     * 获得网络连接是否可用
     *
     * @param context
     * @return
     */
    fun hasNetwork(context: Context): Boolean {
        val con = context
            .getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
        val workinfo = con.activeNetworkInfo
        return !(workinfo == null || !workinfo.isAvailable)
    }

    /**
     * 获取网络信息
     *
     * @param context
     * @return
     */
    fun getActiveNetWork(context: Context): NetworkInfo? {
        // 获取网络连接管理器
        val conn = context
            .getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
        // 获取当前激活的网络
        return conn.activeNetworkInfo
    }

    /**
     * 判断MOBILE网络是否可用
     *
     * @param context
     * @return
     */
    fun isMobileConnected(context: Context?): Boolean {
        if (context != null) {
            val mConnectivityManager = context
                .getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
            val mMobileNetworkInfo = mConnectivityManager
                .getNetworkInfo(ConnectivityManager.TYPE_MOBILE)
            if (mMobileNetworkInfo != null) {
                return mMobileNetworkInfo.isAvailable
            }
        }
        return false
    }

    /**
     * 获取当前网络连接的类型信息
     *
     * @param context
     * @return 0 3G 1 WIFI
     */
    fun getConnectedType(context: Context?): Int {
        if (context != null) {
            val mConnectivityManager = context
                .getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
            val mNetworkInfo = mConnectivityManager
                .activeNetworkInfo
            if (mNetworkInfo != null && mNetworkInfo.isAvailable) {
                return mNetworkInfo.type
            }
        }
        return -1
    }

    /**
     * 检测3G是否连接
     *
     * @return
     */
    private fun is3gConnected(context: Context): Boolean {
        val cm = context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
        if (cm != null) {
            val networkInfo = cm.activeNetworkInfo
            if (networkInfo != null && networkInfo.type == ConnectivityManager.TYPE_MOBILE) {
                return true
            }
        }
        return false
    }
}